import java.awt.*;

/**
 * The Player class extends MovableEnt and creates a Player object
 * 
 * @author      Zachary Bellido
 * @version     0.01
 */
public class Player extends MovableEnt implements StepInterface
{

    private char mySymbol;
    private int health;
    private int numProjectiles;
    private int score;
    
    /**
     * Constructor for objects of class Player
     */
    public Player(char symbol, int coordX, int coordY)
    {
        super(symbol, coordX, coordY);
        Grid.addEnt(this);
        health = 100;
    }
    
    /**
     * Decreases the health of the player
     * @pre        none
     * @param  x   the amount to decrease health by
     * @return     none
     * @post       health == health - x
     */
    public void decreaseHealth(int x)
    {
        health -= x;
    }
    
     /**
     * Gets the HP of Player
     *
     * @param      none
     * @return     none
     */
    public int getHP()
    {
        return health;
    }
    
    /**
     * Sets the HP of the Player
     * 
     * @pre        none
     * @param  hp  the number to set player health to
     * @return     none
     * @post       none
     */
    public void setHP(int hp)
    {
        health = hp;
    }
    
    /**
     * Adds projectiles to the players ammo
     * 
     * @pre        none
     * @param  n   amount to add
     * @return     none
     * @post       none
     */
    public void addProjectiles(int n)
    {
        numProjectiles += n;
    }
    
    /**
     * Removes projectiles from ammo after having fired
     * 
     * @pre        none
     * @param  n   amount to remove from ammo
     * @return     none
     * @post       none
     */
    public void useProjectiles(int n)
    {
        numProjectiles -= n;
    }
    
    /**
     * Gets the number of projectiles the player currently has
     * 
     * @pre        none
     * @param  hp  the number to set player health to
     * @return     none
     * @post       none
     */
    public int getNumProjectiles()
    {
        return numProjectiles;
    }   
 
    /**
     * Preform actions every timer tick
     * 
     * @pre     object must be initialized     
     * @param   none
     * @return  none
     * @post    none    
     */
    public void step(){
        
        if(health <= 0)
        {
            DialogBox.popUp("./images/gameover.jpg");
            System.exit(1);    
        }

        int prevX;  //the x coord before step
        int prevY;  //the y coord before step
        prevX = getCoordX();
        prevY = getCoordY();
        
        //Find net direction based on control states
        int controlVertical = 0;
        int controlHorizontal = 0;
        if (Runner.controlW){
            controlVertical--;
        }
        if (Runner.controlS){
            controlVertical++;
        }
        if (Runner.controlA){
            controlHorizontal--;
        }
        if (Runner.controlD){
            controlHorizontal++;
        }

        //Attempt to move
        moveRestricted(controlHorizontal, controlVertical);
        //Check if player is stuck
        if (this.getCoordX() == prevX && this.getCoordY() == prevY){
            if(Grid.getEntAt(prevX + controlHorizontal, prevY + controlVertical) instanceof Item){
                Item item = (Item)(Grid.getEntAt(prevX + controlHorizontal, prevY + controlVertical));
                //Collect item if found and move anyways
                item.add();
                moveRestricted(controlHorizontal, controlVertical);
            }
        }
        
        //Check projectile controls and spawn projectiles if necessary.
        if(Runner.control8){
            new Projectile('^', getCoordX(), getCoordY()-1, 0);
        }
        if(Runner.control4){
            new Projectile('<', getCoordX()-1, getCoordY(), 3);
        }
        if(Runner.control5){
            new Projectile('v', getCoordX(), getCoordY()+1, 2);
        }
        if(Runner.control6){
            new Projectile('>', getCoordX()+1, getCoordY(), 1);
        }
    }
    
    /**
     * The remove() method removes self from the grid
     *  
     * @pre     
     * @param   none
     * @return  none     
     * @post    removes from the grid
     */
    public void remove(){
        //If you remove the player, nothing good will happen.
    }
    
    /**
     * The reset() method sets back the players stats to default value
     *  
     * @pre     score, health
     * @param   none
     * @return  none     
     * @post    score set to 0 and health set to default health
     */
    public void reset(){
        score = 0;
        health = 100;
    }
    
    /**
     * The updateScore() method updates score of the player
     *  
     * @pre     score
     * @param   int intScore
     * @return  none     
     * @post    increments score with score from param
     */
    public void updateScore(int intScore){
        score += intScore;
    }
}
